package com.lj.eduservice.service.impl;

import com.lj.eduservice.entity.Chapter;
import com.lj.eduservice.entity.Video;
import com.lj.eduservice.entity.dao.ChapterDao;
import com.lj.eduservice.mapper.ChapterMapper;
import com.lj.eduservice.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.eduservice.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author luojing
 * @since 2021-05-06
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterDao> getChapterAndVideoByCourseId(String courseId) {
        return baseMapper.getChapterAndVideoByCourseId(courseId);
    }

    /**
     * 删除章节需要先删除它的小节
     * @param chapterId
     * @return
     */
    @Override
    @Transactional
    public Boolean deleteChapter(String chapterId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("chapter_id",chapterId);
        Collection<Video> videos = videoService.listByMap(params);
        for (Video video : videos) {
           videoService.deleteVideo(video);
        }
        //最后删除章节信息
        this.removeById(chapterId);
        return true;
    }
}
